﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Products
{
    internal class ManufacturerDAO
    {

        #region general

        private static Manufacturer LoadObj(IDataReader datareader)
        {
            Manufacturer obj = new Manufacturer();
            obj.ManufacturerID = MpDataHelp.GetInt(datareader, "ManufacturerID");
            obj.Name = MpDataHelp.GetString(datareader, "Name");
            obj.Description = MpDataHelp.GetString(datareader, "Description");
            obj.Image = MpDataHelp.GetString(datareader, "Image");
            obj.SortOrder = MpDataHelp.GetInt(datareader, "SortOrder");
            obj.IsActive = MpDataHelp.GetBoolean(datareader, "IsActive");
            obj.CreateDate = MpDataHelp.GetDateTime(datareader, "CreateDate");
            return obj;
        }

        #endregion

        public static int Manufacturer_Add(Manufacturer obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Manufacturer_Add");
            db.AddOutParameter(dbCommand, "ManufacturerID", DbType.Int32, obj.ManufacturerID);
            db.AddInParameter(dbCommand, "Name", DbType.String, obj.Name);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Image", DbType.String, obj.Image);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@ManufacturerID"));
            return -1;
        }
        public static int Manufacturer_Update(Manufacturer obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Manufacturer_Update");
            db.AddInParameter(dbCommand, "ManufacturerID", DbType.Int32, obj.ManufacturerID);
            db.AddInParameter(dbCommand, "Name", DbType.String, obj.Name);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Image", DbType.String, obj.Image);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Manufacturer_Delete(int ManufacturerID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Manufacturer_Delete");
            db.AddInParameter(dbCommand, "ManufacturerID", DbType.Int32, ManufacturerID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Manufacturer> Manufacturer_Select(int ManufacturerID, bool IsActive, int SortOrder)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Manufacturer_Select");
            db.AddInParameter(dbCommand, "ManufacturerID", DbType.Int32, ManufacturerID);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, IsActive);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, SortOrder);
            List<Manufacturer> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Manufacturer>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}
